<template>
	<view class="store">
		<view class="head" :style="{height: pHeight + tHeight + 'px'}">
			<view class="xztitle" :style="[{height: pHeight + 'px', top: tHeight + 'px',lineHeight: pHeight + 'px'}]">
				修改分成
				<view class="iconfont icon-dayuhao1" @click="fanhui"></view>
			</view>
		</view>
		<view class="headtop" :style="{marginTop: pHeight + tHeight + 'px'}"></view>
		<view class="form">
			<view class="content clearfix">
				<view class="name fl">合作期限(月)</view>
				<view class="inp fr">
					<input type="text" placeholder="请输入合作期限" @input="qixian" v-model="info.duration" />
				</view>
			</view>
			<view class="content clearfix">
				<view class="name fl">结束时间</view>
				<view class="inp fr">
					<input type="text" placeholder="请输入结束时间" v-model="info.endtime" disabled />
				</view>
			</view>
			<view class="content clearfix">
				<view class="name fl">充电宝分成比例（%）</view>
				<view class="inp fr">
					<input type="text" placeholder="请输入" v-model="info.divided" />
				</view>
			</view>
			<view class="content clearfix">
				<view class="name fl">充电线分成比例（%）</view>
				<view class="inp fr">
					<input type="text" placeholder="请输入" v-model="info.dividedline" />
				</view>
			</view>
			<view class="content contentlast clearfix">
				<view class="name fl">盒子分成比例（%）</view>
				<view class="inp fr">
					<input type="text" placeholder="请输入" v-model="info.dividedbox" />
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="submit" @click="submit">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				bussinid: '',
				info: {
					divided: '',
					duration: '',
					endtime: '',
					dividedline: '',
					dividedbox: '',
				},
				maxdivided: '',
				maxdividedline: '',
				maxdividedbox: '',
			}
		},
		onLoad(options) {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
			}
			
			this.bussinid = options.bussinid;//商户id
			
			//获取商户信息
			this.request({
				url: '/xcx/bussinessinfo',
				data: {
					bussinid: this.bussinid
				},
				method: "POST"
			}).then(res => {
				if (res.data.code == 200) {
					this.info = res.data.data;
					this.maxdivided = res.data.data.maxdivided;
					this.maxdividedline = res.data.data.maxdividedline;
					this.maxdividedbox = res.data.data.maxdividedbox;
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					})
				}
			})
		},
		methods: {
			submit(){
				let bussinid = this.bussinid;
				let divided = this.info.divided;
				let dividedline = this.info.dividedline;
				let dividedbox = this.info.dividedbox;
				let duration = this.info.duration;
				let endtime = this.info.endtime;
				
				if(divided == '' || duration == '' || endtime == ''){
					uni.showToast({
						title: '信息不能为空',
						icon: 'none'
					})
					return false;
				}
				if(parseFloat(divided) >= parseFloat(this.maxdivided)){
					uni.showToast({
						title: '充电宝分成比例不能大于上级分成比例',
						icon: 'none'
					})
					return false;
				}
				
				if(parseFloat(dividedline) >= parseFloat(this.maxdividedline)){
					uni.showToast({
						title: '充电线分成比例不能大于上级分成比例',
						icon: 'none'
					})
					return false;
				}
				
				if(parseFloat(dividedbox) >= parseFloat(this.maxdividedbox)){
					uni.showToast({
						title: '盒子分成比例不能大于上级分成比例',
						icon: 'none'
					})
					return false;
				}
				
				this.request({
					url: '/agent/editbussiness',
					data: {
						bussinessid: bussinid,
						divided: divided,
						duration: duration,
						endtime: endtime,
						dividedstatus: 1,
						dividedline: dividedline,
						dividedbox: dividedbox,
					},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: '成功',
							icon: 'none'
						})
						
						setTimeout(function(){
							uni.reLaunch({
								url: '/subpackageA/index/index'
							})
						},2000)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				})
			},
			qixian(e){
				var month = e.detail.value;
				//查看当前时间月日年
				if(month==''||month==null||month==undefined){
					this.info.endtime == '';
				}else{
					let date = new Date();
					date.setMonth(date.getMonth()+parseInt(month));	
					let year = date.getFullYear();//年
					let month1 = date.getMonth() + 1;//月
					let strDate = date.getDate();//日
					if (month1 < 10) month1 = `0${month1}` // 如果月份是个位数，在前面补0
					  if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数，在前面补0
					this.info.endtime = year+"-"+month1+"-"+strDate;
					this.info.duration = month;
				}
			},
			fanhui() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.store {
		width: 100%;
		margin: 0 auto;
		height: 100vh;
		background: #f9f9ff;
		overflow-y: scroll;

		.head {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);

			.xztitle {
				position: fixed;
				left: 0%;
				top: 0;
				width: 100%;
				text-align: left;
				color: #333333;
				font-size: 32rpx;
				padding-left: 74rpx;
				font-weight: 700;

				text {
					font-size: 24rpx;
				}

				.iconfont {
					position: absolute;
					top: 0;
					left: 30rpx;
					font-size: 44rpx;
				}
			}
		}

		.headtop {
			width: 100%;		
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);
			height: 300rpx;
		}
		
		.form{
			width: 94%;
			margin: 0 auto;
			margin-top: -280rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 0 20rpx;
			box-shadow:rgba(223, 230, 255, 0.67) 0px 0px 6px;
			
			.content{
				height: 92rpx;
				line-height: 92rpx;
				border-bottom: 1rpx solid #D8D8D8;
				
				.name{
					font-size: 28rpx;
					color: #333333;
					width: 60%;
				}
				
				.inp{
					width: 40%;
					text-align: right;
					
					input{
						font-size: 28rpx;
						color: #999999;
						height: 92rpx;
						line-height: 92rpx;
					}
				}
				
				.xuanze{
					width: 60%;
					text-align: right;
					height: 92rpx;
					line-height: 92rpx;
					color: #368BF8;
				}
				
				.img{
					width: 60%;
					text-align: right;
					height: 92rpx;
					line-height: 92rpx;
					
					image{
						width: 108rpx;
						height: 64rpx;
						margin-top: 12rpx;
					}
				}
			}
			
			.contentlast{
				border: none;
			}
			
			.contentdx{
				
				.name{
					font-size: 28rpx;
					color: #333333;
					width: 50%;
				}
				
				.inp{
					width: 40%;
				}
				
				image{
					width: 40rpx;
					height: 40rpx;
					margin-top: 26rpx;
				}
				
				.xuanze{
					width: 40%;
				}
			}
		}
		
		.form1{
			margin-top: 40rpx;
		}
		
		.shoufei{
			width: 94%;
			margin: 0 auto;
			margin-top: 40rpx;
			
			.fangan{
				width: 100%;
				margin: 0 auto;
				margin-bottom: 40rpx;
				background: #FFFFFF;
				border-radius: 20rpx;
				padding: 32rpx 20rpx 22rpx 20rpx;
				
				.top{
					padding-bottom: 20rpx;
					border-bottom: 1rpx solid #D8D8D8;
					
					.title{
						font-size: 28rpx;
						color: #333333;
					}
					
					.edit{
						font-size: 28rpx;
						color: #368BF8;
					}
				}
				
				.bottom{
					
					.text{
						font-size: 24rpx;
						color: #333333;
						margin-top: 20rpx;
					}
				}
			}
		}
		
		.footer{
			width: 100%;
			margin: 0 auto 40rpx;
			margin-top: 60rpx;
			height: 72rpx;
			text-align: center;
			
			.submit{
				width: 240rpx;
				height: 72rpx;
				line-height: 72rpx;
				background: linear-gradient(90deg, #65bdfc, #398ef8);
				margin-left: 248rpx;
				border-radius: 36rpx;
				border-top-left-radius: 0;
				border-bottom-left-radius: 42rpx;
				color: #FFFFFF;
				font-size: 32rpx;
			}
		}
	}
</style>